package com.bw.kotlindemo.kotlin.kvm

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.kotlindemo.java.model.bean.LiveListBean
import com.bw.kotlindemo.java.model.bean.TypeBean
import com.bw.kotlindemo.java.model.net.RetrofitManager
import com.bw.kotlindemo.kotlin.base.KBaseViewModel
import com.bw.kotlindemo.kotlin.bean.KLiveTypeBean
import com.bw.kotlindemo.kotlin.model.KRetrofitManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * desc:直播相关activity
 * create by xushaodong on 2023/4/5
 */
class KLiveVM(application: Application) : KBaseViewModel(application) {
    var liveTypeData = MutableLiveData<KLiveTypeBean>()
    var liveListVM = MutableLiveData<LiveListBean>()
    /**
     * 获取直播类型-使用Rxjava
     * 正常写法
     */
    fun getTypeListByJava() {
        KRetrofitManager.instance
            .apiService
            .getListType()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                Log.e("直播","01 类型："+it.message);
                liveTypeData.value = it
            }
    }

    fun getTypeList() {
        //直接使用协程进行网络请求
        viewModelScope.launch {
            withContext(Dispatchers.IO){
                var response = KRetrofitManager.instance.apiService
                    .getListTypeByKotlin()
                    .execute()
                withContext(Dispatchers.Main){
                    liveTypeData.value = response.body()
                }
            }
        }

        //封装方法
//        request(
//            {
//               getData(KRetrofitManager.instance.apiService.getListTypeByKotlin())
//            },
//            { liveTypeData.value = it},
//            {errorVM.value = it})
    }

    fun getLiveList(liveType: Int, page: Int, size: Int) {

//        KRetrofitManager.instance.apiService
//            .getLiveByType(liveType,page,size)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe {
//                Log.e("直播","01 列表类型："+it.result.toString());
//            }

//        viewModelScope.launch {
//            withContext(Dispatchers.IO){
//                var response = KRetrofitManager.instance.apiService
//                    .getLiveListByKotlin(liveType, page, size)
//                    .execute()
//                withContext(Dispatchers.Main){
//                    liveListVM.value = response.body()
//                }
//            }
//        }

        request(
            {
                getData(KRetrofitManager.instance.apiService.getLiveListByKotlin(liveType,page,size))
            },
            {
                Log.e("直播","直播列表:"+it.message);
                liveListVM.value = it},
            {errorVM.value = it})

    }
}